<template>
  <div>
    <el-tabs v-model="activeName" type="card" class="demo-tabs" @tab-click="handleClick">

      <el-tab-pane label="待出库" name="first">
        <el-form :model="form" inline>
          <el-form-item label="仓库：">
            <el-select v-model="form.warehouseId" style="width: 139px;">
              <el-option :label="item.name" :value="item.id" v-for="item in store.NewHouseList" :key="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="货主:">
            <el-select v-model="form.company" style="width: 139px;">
              <el-option :label="item" :value="item" v-for='(item, index) in outSaleOrderGetPreCompanyListArr'
                :key='index' />
            </el-select>
          </el-form-item>
          <el-form-item label="订单号：">
            <el-input v-model="form.orderOid" clearable style="width: 200px;" />
          </el-form-item>
          <el-form-item label="出库运单号：">
            <el-input v-model="form.orderInvoiceNo" clearable />
          </el-form-item>

          <el-form-item label="打印标签时间：">
            <el-date-picker v-model="preDeliveryFormtimes" type="datetimerange" start-placeholder="开始时间"
              value-format="x" end-placeholder="结束时间" time-format="A hh:mm:ss" clearable />
          </el-form-item>
          <el-form-item label="发货状态">
            <el-select v-model="form.isSend">
              <el-option label="成功" :value="1" />
              <el-option label="失败" :value="2" />
              <el-option label="全部" :value="-1" />
              <el-option label="未发货" :value="0" />
            </el-select>
          </el-form-item>

          <el-form-item label="取货人：">
            <el-select v-model="form.pickPerson">
              <el-option :label="item" :value="item" v-for="(item, index) in outSaleOrdergetPackPersonListArr"
                :key="index" clearable />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleClick">搜索</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-button type="primary" @click="printMyTag(multipleSelection)">批量打印标签</el-button>
        </div>
        <div class="table">
          <el-table :data="tableData" style="width: 1640px" @selection-change="handleSelectionChange" max-height="600">
            <el-table-column type="selection" width="55" />
            <el-table-column label="仓库" width="220">
              <template #default="scope">{{ scope.row.warehouseName }}</template>
            </el-table-column>
            <el-table-column label="货主" width="140">
              <template #default="scope">{{ scope.row.company }}</template>
            </el-table-column>
            <el-table-column label="订单号" width="220">
              <template #default="scope">{{ scope.row.orderOid }}</template>
            </el-table-column>
            <el-table-column label="订单类型" width="180">
              <template #default="scope">{{ scope.row.typeName }}</template>
            </el-table-column>
            <el-table-column label="取货人" width="150">
              <template #default="scope">{{ scope.row.pickPerson }}</template>
            </el-table-column>
            <el-table-column label="打印标签时间" width="220">
              <template #default="scope">{{ scope.row.pickTime }}</template>
            </el-table-column>
            <el-table-column label="出库单号" width="150">
              <template #default="scope">{{ scope.row.orderInvoiceNo }}</template>
            </el-table-column>
            <el-table-column label="出库重量" width="150">
              <template #default="scope">{{ scope.row.outWeight }}</template>
            </el-table-column>
            <el-table-column label="操作" width="200">
              <template #default="scope">
                <el-button type="primary" link size="small" @click="printMyTag(scope.row)">
                  重打标签
                </el-button>
                <el-button link type="primary" size="small" @click.prevent="outSaleOrderbackOnLinePreApi(scope.row)">
                  返回线上采购
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 打印标签取货人 -->
        <el-dialog v-model="TagdialogVisible" width="500">
          <span>
            <el-form :model="Tagform" style="max-width: 500px">
              <el-form-item label="取货人：">
                <el-select v-model="Tagform.getPeopleName" clearable>
                  <el-option :label="item.name" :value="item.name" v-for="item in PickPersonList" :key="item.id" />
                </el-select>
              </el-form-item>
            </el-form>
          </span>
          <template #footer>
            <div class="dialog-footer">
              <el-button @click="TagdialogVisible = false">取消</el-button>
              <el-button type="primary" @click="submitTagPrint(ContentBottom)">
                确定
              </el-button>
            </div>
          </template>
        </el-dialog>
      </el-tab-pane>
      <el-tab-pane label="已出库" name="fourth">
        <el-form :model="OUtList" inline>
          <el-form-item label="仓库：">
            <el-select v-model="OUtList.warehouseId" style="width: 139px;">
              <el-option :label="item.name" :value="item.id" v-for="item in store.NewHouseList" :key="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="货主：">
            <el-select v-model="OUtList.company" style="width: 139px;">
              <el-option :label="item" :value="item" v-for='(item, index) in store.outSaleOrderGetCompanylistTable'
                :key='index' />
            </el-select>
          </el-form-item>
          <el-form-item label="订单号：">
            <el-input v-model="OUtList.orderOid" />
          </el-form-item>
          <el-form-item label="出库运单号：">
            <el-input v-model="OUtList.orderInvoiceNo" />
          </el-form-item>
          <el-form-item label="店铺SKUID：">
            <el-input v-model="OUtList.orderSkuId" clearable />
          </el-form-item>
          <!-- <el-form-item label="拣货单单号：">
            <el-input v-model="OUtList.prePickOrderNum" />
          </el-form-item> -->
          <el-form-item label="打包时间：">
            <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x"
              end-placeholder="结束时间" time-format="A hh:mm:ss" />
          </el-form-item>
          <el-form-item label="发货状态">
            <el-select v-model="OUtList.isSend">
              <el-option label="成功" :value="0" />
              <el-option label="失败" :value="1" />
              <el-option label="全部" :value="-1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleClick">搜索</el-button>
          </el-form-item>
        </el-form>
        <div class="table">
          <el-table :data="tableData" style="width: 1640px" max-height="600">
            <el-table-column label="仓库" width="120">
              <template #default="scope">{{ scope.row.warehouseName }}</template>
            </el-table-column>
            <el-table-column label="货主" width="140">
              <template #default="scope">{{ scope.row.company }}</template>
            </el-table-column>
            <el-table-column label="订单号" width="220">
              <template #default="scope">{{ scope.row.orderOid }}</template>
            </el-table-column>
            <el-table-column label="订单类型" width="180">
              <template #default="scope">{{ scope.row.typeName }}</template>
            </el-table-column>
            <el-table-column label="取货人" width="150">
              <template #default="scope">{{ scope.row.pickPerson }}</template>
            </el-table-column>
            <el-table-column label="打印标签时间" width="220">
              <template #default="scope">{{ scope.row.pickTime }}</template>
            </el-table-column>
            <el-table-column label="打包人" width="220">
              <template #default="scope">{{ scope.row.packPerson }}</template>
            </el-table-column>
            <el-table-column label="打包时间" width="150">
              <template #default="scope">{{ scope.row.packTime }}</template>
            </el-table-column>
            <el-table-column label="出库单号" width="150">
              <template #default="scope">{{ scope.row.orderInvoiceNo }}</template>
            </el-table-column>
            <el-table-column label="出库重量" width="150">
              <template #default="scope">{{ scope.row.outWeight }}</template>
            </el-table-column>
            <!-- <el-table-column label="操作">
              <template #default="scope">
                <el-button link type="primary" size="small" @click.prevent="deleteRow(scope.$index)">
                  出库
                </el-button>
              </template>
            </el-table-column> -->
          </el-table>
        </div>
      </el-tab-pane>
    </el-tabs>

  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup>
import { ref, onBeforeMount, onMounted, onBeforeUnmount } from 'vue'
// outSaleOrderPack, outSaleOrderDelivery,outSaleOrderout
import { getPreOutSaleOrderList, outSaleOrderGetOutSaleOrderList, outSaleOrderbatchPrint, pickSaleOrderGetPickPersonList, outSaleOrderGetPreCompanyList, outSaleOrdergetPackPersonList, outSaleOrderbackOnLinePre } from '@/api/index'
import { ElMessage } from 'element-plus'
import { useAlertsStore } from '@/store/index'
const store = useAlertsStore()
const activeName = ref('first')
const preDeliveryFormtimes = ref([])
const handleClick = (tab, event) => {
  console.log('tab')
  // console.log(tab.props)
  if (tab) {
    activeName.value = tab.props ? tab.props.name : activeName.value
  }
  if (activeName.value === 'first') {
    outSaleOrderGetPreCompanyListApi()
    outSaleOrdergetPackPersonListApi()
    getList()
  } else if (activeName.value === 'fourth') {
    if (times.value) {
      OUtList.value.beginDateTime = times.value[0]
      OUtList.value.endDateTime = times.value[1]
    } else {
      OUtList.value.beginDateTime = ''
      OUtList.value.endDateTime = ''
    }
    getListOut()
  }
}
onBeforeMount(() => {
  store.wareHouseSelect()
  store.outSaleOrderGetCompanylistBtn()
  handleClick()
})
onMounted(() => {
  doConnect()
})
const pageForm = ref({
  pageSize: 10,
  pageNum: 1
})
// 货主
const outSaleOrderGetPreCompanyListArr = ref([])
const outSaleOrderGetPreCompanyListApi = () => {
  outSaleOrderGetPreCompanyList().then(res => {
    console.log('货主', res)
    const { data: { data } } = res
    outSaleOrderGetPreCompanyListArr.value = data
  })
}
const form = ref({
  warehouseId: -1,
  company: '全部',
  orderOid: '',
  orderInvoiceNo: '',
  beginDateTime: '',
  endDateTime: '',
  isSend: -1,
  pickPerson: '全部'
})
// table多选
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 取货人
const outSaleOrdergetPackPersonListArr = ref([])
const outSaleOrdergetPackPersonListApi = () => {
  outSaleOrdergetPackPersonList().then(res => {
    const { data: { data } } = res
    outSaleOrdergetPackPersonListArr.value = data
  })
}
// 未出库列表
const getList = () => {
  if (preDeliveryFormtimes.value.length > 0) {
    form.value.beginDateTime = preDeliveryFormtimes.value[0]
    form.value.endDateTime = preDeliveryFormtimes.value[1]
  }
  getPreOutSaleOrderList(Object.assign({}, pageForm.value, form.value)).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// const ToBeReviewed = ref({})
const OUtList = ref({
  warehouseId: -1,
  company: '全部',
  orderOid: '',
  orderInvoiceNo: '',
  beginDateTime: '',
  endDateTime: '',
  orderSkuId: '',
  isSend: -1
})
const getListOut = () => {
  outSaleOrderGetOutSaleOrderList(Object.assign({}, pageForm.value, OUtList.value)).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const tableData = ref([])
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  pageForm.value.pageSize = val
  handleClick()
}
const handleCurrentChange = (val) => {
  pageForm.value.pageNum = val
  handleClick()
}

// // 打包
// // const tableDataDD = ref([])
// const takeaway = (row) => {
//   outSaleOrderPack({ id: row.id }).then(res => {
//     ElMessage.success('操作成功')
//     getList()
//   })
// }
// // 批量打包
// const takeawayEvery = () => {
//   if (multipleSelection.value.length > 0) {
//     multipleSelection.value.forEach(e => {
//       outSaleOrderPack({ id: e.id }).then(res => {
//       })
//     })
//     ElMessage.success('操作成功')
//     getList()
//   } else {
//     ElMessage.error('先选择数据')
//   }
// }

// // 发货
// const dispatchBtn = (row) => {
//   outSaleOrderDelivery({ id: row.id }).then(res => {
//     ElMessage.success('操作成功')
//     getList()
//   })
// }
// // 批量发货
// const dispatchEvery = () => {
//   if (multipleSelection.value.length > 0) {
//     multipleSelection.value.forEach(e => {
//       outSaleOrderDelivery({ id: e.id }).then(res => {
//       })
//     })
//     ElMessage.success('操作成功')
//     getList()
//   } else {
//     ElMessage.error('先选择数据')
//   }
// }

// const loading = ref(false)
// // 出库
// const outboundBtn = (row) => {
//   loading.value = true
//   outSaleOrderout({ ids: [row.id] }).then(res => {
//     ElMessage.success('操作成功')
//     getList()
//     loading.value = false
//   })
// }
// // 批量出库
// const outboundEvery = () => {
//   loading.value = true
//   if (multipleSelection.value.length > 0) {
//     const ids = []
//     multipleSelection.value.forEach(e => {
//       ids.push(e.id)
//       console.log(ids)
//     })
//     outSaleOrderout({ ids: ids }).then(res => {
//       loading.value = false
//       // ElMessage.success('操作成功')
//     })
//     ElMessage.success('操作成功')
//     getList()
//   } else {
//     ElMessage.error('先选择数据')
//   }
// }
const times = ref()
// 返回线上
const outSaleOrderbackOnLinePreApi = (row) => {
  outSaleOrderbackOnLinePre({ id: row.id }).then(res => {
    ElMessage.success('操作成功 ')
    handleClick()
  })
}
// 批量打印标签

const Tagform = ref({})
const PickPersonList = ref([])
const TagdialogVisible = ref(false)
const ContentBottom = ref('')
const printMyTag = async (row) => {
  const printList = await pickSaleOrderGetPickPersonList()
  console.log('printList')
  const { data: { data } } = printList
  console.log(data)
  data.forEach(e => {
    if (e.id === 1) {
      Tagform.value.getPeopleName = e.name
    }
  })
  PickPersonList.value = data
  ContentBottom.value = row
  TagdialogVisible.value = true
}
const submitTagPrint = (row) => {
  if (Object.prototype.toString.call(row) === '[object Object]') {
    outSaleOrderbatchPrint({
      name: Tagform.value.getPeopleName,
      ids: [row.id]
    }).then(res => {
      const { data: { data } } = res
      console.log(data)
      if (localStorage.getItem('PrinterSetListKW')) {
        const lists = JSON.parse(localStorage.getItem('PrinterSetListKW'))
        const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '65', height: '38', padding: '2', barcodeHeight: '8', barcodeMargin: '1.8', fontSize: '8', showOrderName: '0', left: '2', datas: data[0] }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/TagMy.xml' }] }] } }
        store.socket.send(JSON.stringify(val))
        handleClick()
        TagdialogVisible.value = false
      } else {
        ElMessage.success('先设置打印机')
      }
    })
  } else if (Object.prototype.toString.call(row) === '[object Array]') {
    const a = []
    if (row.length > 0) {
      row.forEach(element => {
        a.push(element.id)
      })
      outSaleOrderbatchPrint({
        name: Tagform.value.getPeopleName,
        ids: a
      }).then(res => {
        const { data: { data } } = res
        console.log(data)
        let a = 1
        if (localStorage.getItem('PrinterSetListKW')) {
          data.forEach(element => {
            a++
            const lists = JSON.parse(localStorage.getItem('PrinterSetListKW'))
            const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + a + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '65', height: '42', padding: '2', barcodeHeight: '8', barcodeMargin: '1.8', fontSize: '8', showOrderName: '0', left: '2', datas: element }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/TagMy.xml' }] }] } }
            store.socket.send(JSON.stringify(val))
            handleClick()
            TagdialogVisible.value = false
          })
        } else {
          ElMessage.error('先设置打印机')
        }
      })
    } else {
      ElMessage.error('请先选择数据')
    }
  }
}
const doConnect = () => {
  console.log('d打开了不')
  store.socket = new WebSocket('ws://localhost:13528')
  // 打开Socket
  store.socket.onopen = function (event) {
    console.log('连接了')
    // 监听消息
    store.socket.onmessage = function (event) {
      console.log('接受了信息')
      // localStorage.setItem('printerproperties', event.data)
    }
    // 监听Socket的关闭
    store.socket.onclose = function (event) {
      // console.log('Client notified socket has closed', event)
    }
  }
}
// 销毁
onBeforeUnmount(() => {
  console.log('销毁')
  store.destroyed()
})
</script>

<style lang='less' scoped>
.demo-tabs>.el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>
